//This script produces Figure A.1 in the Appendix of the Cambridge Element

//We used Stata/SE 18.0 for Mac (Intel 64-bit), Revision 25 Apr 2023

//Load CSES modules 1-4 data
use "cses_imd.dta", clear

//Calculate average like-dislikes of leaders and parties
replace IMD3008_A = . if IMD3008_A > 10
replace IMD3008_B = . if IMD3008_B > 10
replace IMD3008_C = . if IMD3008_C > 10
replace IMD3008_D = . if IMD3008_D > 10
replace IMD3008_E = . if IMD3008_E > 10
replace IMD3008_F = . if IMD3008_F > 10
replace IMD3008_G = . if IMD3008_G > 10
replace IMD3008_H = . if IMD3008_H > 10
replace IMD3008_I = . if IMD3008_I > 10

replace IMD3009_A = . if IMD3009_A > 10
replace IMD3009_B = . if IMD3009_B > 10
replace IMD3009_C = . if IMD3009_C > 10
replace IMD3009_D = . if IMD3009_D > 10
replace IMD3009_E = . if IMD3009_E > 10
replace IMD3009_F = . if IMD3009_F > 10
replace IMD3009_G = . if IMD3009_G > 10
replace IMD3009_H = . if IMD3009_H > 10
replace IMD3009_I = . if IMD3009_I > 10

egen like_parties = rowmean(IMD3008_A IMD3008_B IMD3008_C IMD3008_D IMD3008_E IMD3008_F IMD3008_G IMD3008_H IMD3008_I)
egen like_leaders = rowmean(IMD3009_A IMD3009_B IMD3009_C IMD3009_D IMD3009_E IMD3009_F IMD3009_G IMD3009_H IMD3009_I)

//Collapse to TSCS level
rename IMD1008_YEAR year
rename IMD1006_NAM country
collapse (mean) like_parties like_leaders, by(country year)

//Save data
save "cses_tscs_1-4.dta", replace


//Load CSES module 5 data
use "cses5.dta", clear

//Calculate average like-dislikes of leaders and parties
replace E3017_A = . if E3017_A > 10
replace E3017_B = . if E3017_B > 10
replace E3017_C = . if E3017_C > 10
replace E3017_D = . if E3017_D > 10
replace E3017_E = . if E3017_E > 10
replace E3017_F = . if E3017_F > 10
replace E3017_G = . if E3017_G > 10
replace E3017_H = . if E3017_H > 10
replace E3017_I = . if E3017_I > 10

replace E3018_A = . if E3018_A > 10
replace E3018_B = . if E3018_B > 10
replace E3018_C = . if E3018_C > 10
replace E3018_D = . if E3018_D > 10
replace E3018_E = . if E3018_E > 10
replace E3018_F = . if E3018_F > 10
replace E3018_G = . if E3018_G > 10
replace E3018_H = . if E3018_H > 10
replace E3018_I = . if E3018_I > 10

egen like_parties = rowmean(E3017_A E3017_B E3017_C E3017_D E3017_E E3017_F E3017_G E3017_H E3017_I)
egen like_leaders = rowmean(E3018_A E3018_B E3018_C E3018_D E3018_E E3018_F E3018_G E3018_H E3018_I)

//Collapse to TSCS level
rename E5026_3 year
rename E1006_NAM country
collapse (mean) like_parties like_leaders, by(country year)

//Merge with modules 1-4
append using "cses_tscs_1-4.dta"
sort country year

//Drop data not needed
drop if country == "Albania"
drop if country == "Argentina"
drop if country == "Belarus"
drop if country == "Costa Rica"
drop if country == "Croatia"
drop if country == "Estonia"
drop if country == "Hong Kong"
drop if country == "Kyrgyzstan"
drop if country == "Lithuania"
drop if country == "Montenegro"
drop if country == "Russian Federation"
drop if country == "Serbia"
drop if country == "Ukraine"
drop if country == "Uruguay"
drop if country == "Kenya"
drop if country == "Tunisia"
replace country = "United States" if country == "United States of America"


//Plot like-dislike of parties and leaders over time and save as Figure A.1
twoway (connected like_parties year) (connected like_leaders year), by(country, note("")) xtitle("Year") ytitle("Average likability") legend(order(1 "Political parties" 2 "Political leaders")) legend(rows(1)) by(country) scheme(lean2) subtitle(, size(large))
graph export "FigureA1.png", width(2500) replace

